import WeDisplayShaders from "../Display/WeDisplayShaders"

export const fragmentMomentum = `
 precision highp float;
 in vec2 v_textureCoordinates;

 vec4 getCurrent(float x, float y){
    float gridSize = Weget_numLon();
    vec2 uv = v_textureCoordinates + vec2(x,y) / gridSize;
    return getCurrentWater(uv);
 }
 vec4 getLast(float x, float y){
     float gridSize = Weget_numLon();
     vec2 uv = v_textureCoordinates + vec2(x,y) / gridSize;
     return getLastWater(uv);
 }

 float getHeight(float x,float y){
     float gridSize = Weget_numLon();
     vec2 uv = v_textureCoordinates + vec2(x,y) / gridSize;
     return getTerrainHeight(uv);
 }

 vec3 exchange(float g1, float l1, float currentW, float x, float y){
  float g2 = getHeight(x,y);
  float w = getCurrent(x, y).x;
  float change = (g2 + w) - (g1 + l1);
  change = clamp(change * 1.00, -currentW * 0.25, w * 0.25);
  return vec3(change, vec2(-x * change, -y * change));
}

 void main(void){
    float g = getHeight(0.0,0.0);
    vec3 l = getLast(0.0, 0.0).xyz;
    vec3 c = getCurrent(0.0, 0.0).xyz;

    vec3 v = exchange(g, l.x, c.x,  1.0,  0.0) +
            exchange(g, l.x, c.x, -1.0,  0.0) +
            exchange(g, l.x, c.x,  0.0,  1.0) +
            exchange(g, l.x, c.x,  0.0, -1.0);

    v *= 0.25;

    vec3 n = c + v;

    out_FragColor = vec4(n,1.0);
 }
`

export const fragmentCycle = `
 precision highp float;
 in vec2 v_textureCoordinates;
 ${WeDisplayShaders.WeCartesian3FromRadians}
  vec2 get(float x, float y){
      float gridSize = Weget_numLon();
      vec2 uv = v_textureCoordinates + vec2(x,y) / gridSize;
      return getTmp(uv).yz;
  }

  float getRainPoint(vec4 circle,vec3 gridPos){
    float rain = Weget_rain();
    float distance = length(gridPos - we_cartesian3FromRadians(vec3(circle.xy,0.0)));
    if(distance < circle.z) {
      return rain + circle.w;
    }
    return 0.0;
  }
 void main(void){
    float rain = Weget_rain();
    float maxWaterHeight = Weget_maxWaterHeight();
    vec2 uv = v_textureCoordinates;
    vec3 water = texture(tmp,uv).rgb;

    uv.y = 1.0 - uv.y;

    float evaporate = Weget_evaporate();

    //
    vec4 extent = Weget_extent();
    vec2 ws = extent.xy;
    vec2 en = extent.zw;
    vec2 lonlat = ws + uv * (en - ws);

    vec3 gridPos = we_cartesian3FromRadians(vec3(lonlat,0.0));

    //
    water.x += rain;
    water.x += getRainPoint(Weget_rainCircle1(),gridPos);
    water.x += getRainPoint(Weget_rainCircle2(),gridPos);
    water.x += getRainPoint(Weget_rainCircle3(),gridPos);
    water.x += getRainPoint(Weget_rainCircle4(),gridPos);
    water.x += getRainPoint(Weget_rainCircle5(),gridPos);


    float e = 0.925;
    water.x *= mix(1.0, e, evaporate);
    water.x = clamp(water.x, 0.0, maxWaterHeight);

    water.yz = (
                get(-1.0, -1.0)*1.0  + get(0.0, -1.0)*1.4 + get(1.0, -1.0)*1.0 +
                get(-1.0,  0.0)*1.4  + water.yz * 300.0   + get(1.0, -1.0)*1.4 +
                get(-1.0, -1.0)*1.0  + get(0.0, -1.0)*1.4 + get(1.0, -1.0)*1.0
            ) * 0.98 * (1.0/( 300.0 + 1.4 * 4.0 + 4.0));

    out_FragColor = vec4(water,0.0);
  }
`

export const fragmentFlow = `
  uniform sampler2D flows;
  in vec2 v_textureCoordinates;
  void main(void){
      float speedFactor = Weget_speedFactor();
      float maxWaterHeight = Weget_maxWaterHeight();
      vec2 uv = v_textureCoordinates;
      vec2 pos = texture(flows, uv).xy;
      vec3 w = getCurrentWater(uv).xyz;
      vec2 vel = (w.yz * 0.001 * speedFactor)/(w.x * 1.+ 0.001);
      out_FragColor = vec4(pos + vel, 0.0, 1.0);
  }
`

export const fragmentCopy = `
  uniform sampler2D source;
  in vec2 v_textureCoordinates;
  void main(void){
      out_FragColor = texture(source, v_textureCoordinates);
  }
`
export default {}
